<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>字符串的format方法 | </title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    
    <link rel="prev" href="./chapter05.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6"
        data-chapter-title="字符串的format方法"
        data-filepath="chapter06.md"
        data-basepath="."
        data-revision="Tue Feb 04 2020 11:24:25 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        字符串的表示方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        字符串的下标和切片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        字符串的常见操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        字符集和编码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        成员运算符
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        字符串的format方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" ></a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5B57;&#x7B26;&#x4E32;&#x7684;format&#x65B9;&#x6CD5;">&#x5B57;&#x7B26;&#x4E32;&#x7684;format&#x65B9;&#x6CD5;</h1>
<h2 id="1-&#x6982;&#x5FF5;">1. &#x6982;&#x5FF5;:</h2>
<ul>
<li><code>str.format()</code> &#x65B9;&#x6CD5;&#x901A;&#x8FC7;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5927;&#x62EC;&#x53F7;<code>{}</code> &#x6765;&#x8BC6;&#x522B;&#x66FF;&#x6362;&#x5B57;&#x6BB5; <code>replacement field</code>&#xFF0C;&#x4ECE;&#x800C;&#x5B8C;&#x6210;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x683C;&#x5F0F;&#x5316;&#x3002;</li>
<li><code>&#x66FF;&#x6362;&#x5B57;&#x6BB5;</code> &#x7531;&#x5B57;&#x6BB5;&#x540D; <code>field name</code> &#x548C;&#x8F6C;&#x6362;&#x5B57;&#x6BB5; <code>conversion field</code> &#x4EE5;&#x53CA;&#x683C;&#x5F0F;&#x8BF4;&#x660E;&#x7B26; <code>format specifier</code> &#x7EC4;&#x6210;&#xFF0C;&#x5373;&#x4E00;&#x822C;&#x5F62;&#x5F0F;&#x4E3A; {&#x5B57;&#x6BB5;&#x540D;!&#x8F6C;&#x6362;&#x5B57;&#x6BB5;:&#x683C;&#x5F0F;&#x8BF4;&#x660E;&#x7B26;}&#x3002;</li>
<li>&#x5B57;&#x6BB5;&#x540D;&#x5206;&#x4E3A;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D; <code>simple field name</code> &#x548C;&#x590D;&#x5408;&#x5B57;&#x6BB5;&#x540D; <code>compound field name</code>&#x3002;&#x800C;&#x8F6C;&#x6362;&#x5B57;&#x6BB5;&#x548C;&#x683C;&#x5F0F;&#x8BF4;&#x660E;&#x7B26;&#x90FD;&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;</li>
</ul>
<h2 id="2-&#x5B57;&#x6BB5;&#x540D;">2. &#x5B57;&#x6BB5;&#x540D;</h2>
<p>form&#x7684;&#x5B8C;&#x6574;&#x683C;&#x5F0F;&#x662F;<span style="color:red">{&#x5B57;&#x6BB5;&#x540D;!&#x8F6C;&#x6362;&#x5B57;&#x7B26;:&#x683C;&#x5F0F;&#x8BF4;&#x660E;&#x7B26;}</span>&#x3002;&#x5176;&#x4E2D;&#x5B57;&#x6BB5;&#x540D;&#x5E08;&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x800C;&#x4E14;&#x53EF;&#x4EE5;&#x5206;&#x4E3A;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x548C;&#x590D;&#x5408;&#x5B57;&#x6BB5;&#x540D;&#x3002;</p>
<h3 id="21-&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;">2.1 &#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;</h3>
<p>&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x7531;&#x4E09;&#x4E2D;&#x5199;&#x6CD5;:</p>
<ul>
<li>&#x7701;&#x7565;&#x5B57;&#x6BB5;&#x540D;:<code>{}</code></li>
<li>&#x4F7F;&#x7528;&#x975E;&#x8D1F;&#x5341;&#x8FDB;&#x5236;&#x6574;&#x6570;<code>{0}</code></li>
<li>&#x53D8;&#x91CF;&#x540D;{name}</li>
</ul>
<h4 id="211-&#x7701;&#x7565;&#x5B57;&#x6BB5;&#x540D;">2.1.1 &#x7701;&#x7565;&#x5B57;&#x6BB5;&#x540D;</h4>
<p>&#x5927;&#x62EC;&#x53F7;&#x5185;&#x7701;&#x7565;&#x5B57;&#x6BB5;&#x540D;&#xFF0C;&#x4F20;&#x9012;<strong>&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</strong>&#x3002;</p>
<ul>
<li>&#x66FF;&#x6362;&#x5B57;&#x6BB5;&#x5F62;&#x5F0F;&#xFF1A; <code>{}</code></li>
<li>&#x6CE8;&#x610F;&#xFF1A;&#x5927;&#x62EC;&#x53F7;&#x4E2A;&#x6570;&#x53EF;&#x4EE5;&#x5C11;&#x4E8E;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;&#xFF0C;&#x53CD;&#x4E4B;&#x4E0D;&#x7136;&#x3002;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x7701;&#x7565;&#x5B57;&#x6BB5;&#x540D;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x53EB;{}&#xFF0C;&#x4ECA;&#x5E74;{}&#x5C81;&#x3002;&apos;</span>.format(<span class="hljs-string">&apos;&#x5C0F;&#x660E;&apos;</span>, <span class="hljs-number">18</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x53EB;&#x5C0F;&#x660E;&#xFF0C;&#x4ECA;&#x5E74;18&#x5C81;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x5927;&#x62EC;&#x53F7;&#x4E2A;&#x6570;&#x53EF;&#x4EE5;&#x5C11;&#x4E8E;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x7231;&#x5403;{}&#x548C;{}&#x3002;&apos;</span>.format(<span class="hljs-string">&apos;&#x9999;&#x8549;&apos;</span>, <span class="hljs-string">&apos;&#x82F9;&#x679C;&apos;</span>, <span class="hljs-string">&apos;&#x5927;&#x9E2D;&#x68A8;&apos;</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x7231;&#x5403;&#x9999;&#x8549;&#x548C;&#x82F9;&#x679C;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x5927;&#x62EC;&#x53F7;&#x4E2A;&#x6570;&#x591A;&#x4E8E;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;&#x5219;&#x4F1A;&#x62A5;&#x9519;</span>
<span class="hljs-comment"># print(&apos;&#x6211;&#x8FD8;&#x5403;{}&#x548C;{}&#x3002;&apos;.format(&apos;&#x897F;&#x7EA2;&#x67FF;&apos;))</span>
<span class="hljs-string">&quot;&quot;&quot;
IndexError: tuple index out of range
&quot;&quot;&quot;</span>
</code></pre>
<h4 id="212-&#x6570;&#x5B57;&#x5B57;&#x6BB5;&#x540D;">2.1.2 &#x6570;&#x5B57;&#x5B57;&#x6BB5;&#x540D;</h4>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x7684;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x3002;</p>
<ul>
<li>&#x6570;&#x5B57;&#x5FC5;&#x987B;&#x662F;&#x5927;&#x4E8E;&#x7B49;&#x4E8E; 0 &#x7684;&#x6574;&#x6570;&#x3002;</li>
<li>&#x5E26;&#x6570;&#x5B57;&#x7684;&#x66FF;&#x6362;&#x5B57;&#x6BB5;&#x53EF;&#x4EE5;&#x91CD;&#x590D;&#x4F7F;&#x7528;&#x3002;</li>
<li>&#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x7684;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x76F8;&#x5F53;&#x4E8E;&#x628A; format &#x4E2D;&#x7684;&#x6240;&#x6709;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x6574;&#x4F53;&#x5F53;&#x4F5C;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#xFF0C;&#x901A;&#x8FC7;&#x5B57;&#x6BB5;&#x540D;&#x4E2D;&#x7684;&#x6570;&#x5B57;&#x8FDB;&#x884C;&#x53D6;&#x503C;&#x3002;&#x5373; {0} &#x7B49;&#x4EF7;&#x4E8E; tuple[0]&#xFF0C;&#x6240;&#x4EE5;&#x5927;&#x62EC;&#x53F7;&#x5185;&#x7684;&#x6570;&#x5B57;&#x4E0D;&#x80FD;&#x8D8A;&#x754C;&#x3002;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x901A;&#x8FC7;&#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x7684;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</span>
print(<span class="hljs-string">&apos;&#x8EAB;&#x9AD8;{0}&#xFF0C;&#x5BB6;&#x4F4F;{1}&#x3002;&apos;</span>.format(<span class="hljs-number">1.8</span>, <span class="hljs-string">&apos;&#x94DC;&#x9523;&#x6E7E;&apos;</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x8EAB;&#x9AD8;1.8&#xFF0C;&#x5BB6;&#x4F4F;&#x94DC;&#x9523;&#x6E7E;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x7684;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x53EF;&#x4EE5;&#x91CD;&#x590D;&#x4F7F;&#x7528;&#x3002;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x7231;{0}&#x3002;\n&#x5979;&#x4ECA;&#x5E74;{1}&#x3002;\n&#x6211;&#x4E5F;&#x7231;{0}&#x3002;&apos;</span>.format(<span class="hljs-string">&apos;&#x963F;&#x9999;&apos;</span>, <span class="hljs-number">17</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x7231;&#x963F;&#x9999;&#x3002;
&#x5979;&#x4ECA;&#x5E74;17&#x3002;
&#x6211;&#x4E5F;&#x7231;&#x963F;&#x9999;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x4F53;&#x4F1A;&#x628A;&#x6240;&#x6709;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x6574;&#x4F53;&#x5F53;&#x6210;&#x5143;&#x7EC4;&#x6765;&#x53D6;&#x503C;</span>
print(<span class="hljs-string">&apos;&#x963F;&#x9999;&#x7231;&#x5403;{1}&#x3001;{3}&#x548C;{0}&#x3002;&apos;</span>.format(
    <span class="hljs-string">&apos;&#x69B4;&#x83B2;&apos;</span>, <span class="hljs-string">&apos;&#x81ED;&#x8C46;&#x8150;&apos;</span>, <span class="hljs-string">&apos;&#x76AE;&#x86CB;&apos;</span>, <span class="hljs-string">&apos;&#x9CB1;&#x9C7C;&#x7F50;&#x5934;&apos;</span>, <span class="hljs-string">&apos;&#x87BA;&#x72EE;&#x7C89;&apos;</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x963F;&#x9999;&#x7231;&#x5403;&#x81ED;&#x8C46;&#x8150;&#x3001;&#x9CB1;&#x9C7C;&#x7F50;&#x5934;&#x548C;&#x69B4;&#x83B2;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x5C1D;&#x8BD5;&#x4E00;&#x4E0B;&#x8D8A;&#x754C;&#x9519;&#x8BEF;</span>
<span class="hljs-comment"># print(&apos;{1}&apos;.format(&apos;&#x9519;&#x8BEF;&#x7528;&#x6CD5;&apos;))</span>
<span class="hljs-string">&quot;&quot;&quot;
IndexError: tuple index out of range
&quot;&quot;&quot;</span>
</code></pre>
<h4 id="213-&#x53D8;&#x91CF;&#x5B57;&#x6BB5;&#x540D;">2.1.3 &#x53D8;&#x91CF;&#x5B57;&#x6BB5;&#x540D;</h4>
<p>&#x4F7F;&#x7528;<strong>&#x53D8;&#x91CF;&#x540D;&#x5F62;&#x5F0F;</strong>&#x7684;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x4F20;&#x9012;<strong>&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;</strong>&#x3002;</p>
<ul>
<li>&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x7684;&#x4F4D;&#x7F6E;&#x53EF;&#x4EE5;&#x968F;&#x610F;&#x8C03;&#x6362;&#x3002;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4F7F;&#x7528;&#x53D8;&#x91CF;&#x540D;&#x5F62;&#x5F0F;&#x7684;&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x4F20;&#x9012;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x5927;&#x54E5;&#x662F;{name}&#xFF0C;&#x4ECA;&#x5E74;{age}&#x5C81;&#x3002;&apos;</span>.format(name=<span class="hljs-string">&apos;&#x963F;&#x98DE;&apos;</span>, age=<span class="hljs-number">20</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x5927;&#x54E5;&#x662F;&#x963F;&#x98DE;&#xFF0C;&#x4ECA;&#x5E74;20&#x5C81;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x7684;&#x987A;&#x5E8F;&#x53EF;&#x4EE5;&#x968F;&#x610F;&#x8C03;&#x6362;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x5927;&#x54E5;&#x662F;{name}&#xFF0C;&#x4ECA;&#x5E74;{age}&#x5C81;&#x3002;&apos;</span>.format(age=<span class="hljs-number">20</span>, name=<span class="hljs-string">&apos;&#x963F;&#x98DE;&apos;</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x5927;&#x54E5;&#x662F;&#x963F;&#x98DE;&#xFF0C;&#x4ECA;&#x5E74;20&#x5C81;&#x3002;
&quot;&quot;&quot;</span>
</code></pre>
<h4 id="214-&#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x7684;&#x6DF7;&#x5408;&#x4F7F;&#x7528;">2.1.4 &#x7B80;&#x5355;&#x5B57;&#x6BB5;&#x540D;&#x7684;&#x6DF7;&#x5408;&#x4F7F;&#x7528;</h4>
<ul>
<li>&#x6DF7;&#x5408;&#x4F7F;&#x7528;&#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x548C;&#x53D8;&#x91CF;&#x540D;&#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x6BB5;&#x540D;&#xFF0C;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x548C;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x3002;</li>
<li>&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x4F4D;&#x4E8E;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x4E4B;&#x540E;&#x3002;</li>
<li>&#x6DF7;&#x5408;&#x4F7F;&#x7528;&#x65F6;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x6570;&#x5B57;&#x3002;</li>
<li>&#x7701;&#x7565;&#x5B57;&#x6BB5;&#x540D; <code>{}</code> &#x4E0D;&#x80FD;&#x548C;&#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x6BB5;&#x540D; <code>{&#x975E;&#x8D1F;&#x6574;&#x6570;}</code> &#x540C;&#x65F6;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x6DF7;&#x5408;&#x4F7F;&#x7528;&#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x548C;&#x53D8;&#x91CF;&#x540D;&#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x6BB5;&#x540D;</span>
<span class="hljs-comment"># &#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x548C;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;</span>
print(<span class="hljs-string">&apos;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5173;&#x4E8E;{0}&#x3001;{1}&#x548C;{girl}&#x7684;&#x6545;&#x4E8B;&#x3002;&apos;</span>.format(
    <span class="hljs-string">&apos;&#x5C0F;&#x660E;&apos;</span>, <span class="hljs-string">&apos;&#x963F;&#x98DE;&apos;</span>, girl=<span class="hljs-string">&apos;&#x963F;&#x9999;&apos;</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5173;&#x4E8E;&#x5C0F;&#x660E;&#x3001;&#x963F;&#x98DE;&#x548C;&#x963F;&#x9999;&#x7684;&#x6545;&#x4E8B;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x4F46;&#x662F;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x4F4D;&#x4E8E;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x4E4B;&#x540E;</span>
<span class="hljs-comment"># print(&apos;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5173;&#x4E8E;{0}&#x3001;{1}&#x548C;{girl}&#x7684;&#x6545;&#x4E8B;&#x3002;&apos;.format(</span>
    <span class="hljs-comment"># &apos;&#x5C0F;&#x660E;&apos;, girl=&apos;&#x963F;&#x9999;&apos; , &apos;&#x963F;&#x98DE;&apos;))</span>
<span class="hljs-string">&quot;&quot;&quot;
SyntaxError: positional argument follows keyword argument
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x6570;&#x5B57;&#x4E5F;&#x53EF;&#x4EE5;&#x7701;&#x7565;</span>
print(<span class="hljs-string">&apos;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5173;&#x4E8E;{}&#x3001;{}&#x548C;{girl}&#x7684;&#x6545;&#x4E8B;&#x3002;&apos;</span>.format(
    <span class="hljs-string">&apos;&#x5C0F;&#x660E;&apos;</span>, <span class="hljs-string">&apos;&#x963F;&#x98DE;&apos;</span>, girl=<span class="hljs-string">&apos;&#x963F;&#x9999;&apos;</span>))

<span class="hljs-comment"># &#x4F46;&#x662F;&#x7701;&#x7565;&#x5B57;&#x6BB5;&#x540D;&#x4E0D;&#x80FD;&#x548C;&#x6570;&#x5B57;&#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x6BB5;&#x540D;&#x540C;&#x65F6;&#x51FA;&#x73B0;</span>
<span class="hljs-comment"># print(&apos;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5173;&#x4E8E;{}&#x3001;{1}&#x548C;{girl}&#x7684;&#x6545;&#x4E8B;&#x3002;&apos;.format(</span>
<span class="hljs-comment">#     &apos;&#x5C0F;&#x660E;&apos;, &apos;&#x963F;&#x98DE;&apos;, girl=&apos;&#x963F;&#x9999;&apos;))</span>
<span class="hljs-string">&quot;&quot;&quot;
ValueError: cannot switch from automatic field numbering to manual field specification
&quot;&quot;&quot;</span>
</code></pre>
<h4 id="215-&#x4F7F;&#x7528;&#x5143;&#x7EC4;&#x548C;&#x5B57;&#x5178;&#x4F20;&#x53C2;">2.1.5 &#x4F7F;&#x7528;&#x5143;&#x7EC4;&#x548C;&#x5B57;&#x5178;&#x4F20;&#x53C2;</h4>
<p><code>str.format()</code> &#x65B9;&#x6CD5;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>*&#x5143;&#x7EC4;</code> &#x548C; <code>**&#x5B57;&#x5178;</code> &#x7684;&#x5F62;&#x5F0F;&#x4F20;&#x53C2;&#xFF0C;&#x4E24;&#x8005;&#x53EF;&#x4EE5;&#x6DF7;&#x5408;&#x4F7F;&#x7528;&#x3002;
&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x3001;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x3001;<code>*&#x5143;&#x7EC4;</code> &#x548C; <code>**&#x5B57;&#x5178;</code> &#x4E5F;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x4F7F;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x8981;&#x5728;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x524D;&#x9762;&#xFF0C;<code>*&#x5143;&#x7EC4;</code> &#x8981;&#x5728; <code>**&#x5B57;&#x5178;</code> &#x524D;&#x9762;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4F7F;&#x7528;&#x5143;&#x7EC4;&#x4F20;&#x53C2;</span>
infos = <span class="hljs-string">&apos;&#x94A2;&#x94C1;&#x4FA0;&apos;</span>, <span class="hljs-number">66</span>, <span class="hljs-string">&apos;&#x5C0F;&#x8FA3;&#x6912;&apos;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x662F;{}&#xFF0C;&#x8EAB;&#x4EF7;{}&#x4EBF;&#x3002;&apos;</span>.format(*infos))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x662F;&#x94A2;&#x94C1;&#x4FA0;&#xFF0C;&#x8EAB;&#x5BB6;66&#x4EBF;&#x3002;
&quot;&quot;&quot;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x662F;{2}&#xFF0C;&#x8EAB;&#x4EF7;{1}&#x4EBF;&#x3002;&apos;</span>.format(*infos))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x662F;&#x5C0F;&#x8FA3;&#x6912;&#xFF0C;&#x8EAB;&#x5BB6;66&#x4EBF;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x4F7F;&#x7528;&#x5B57;&#x5178;&#x4F20;&#x53C2;</span>
venom = {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;&#x6BD2;&#x6DB2;&apos;</span>, <span class="hljs-string">&apos;weakness&apos;</span>: <span class="hljs-string">&apos;&#x706B;&apos;</span>}
print(<span class="hljs-string">&apos;&#x6211;&#x662F;{name}&#xFF0C;&#x6211;&#x6015;{weakness}&#x3002;&apos;</span>.format(**venom))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x662F;&#x6BD2;&#x6DB2;&#xFF0C;&#x6211;&#x6015;&#x706B;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x540C;&#x65F6;&#x4F7F;&#x7528;&#x5143;&#x7EC4;&#x548C;&#x5B57;&#x5178;&#x4F20;&#x53C2;</span>
hulk = <span class="hljs-string">&apos;&#x7EFF;&#x5DE8;&#x4EBA;&apos;</span>, <span class="hljs-string">&apos;&#x62F3;&#x5934;&apos;</span>
captain = {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;&#x7F8E;&#x56FD;&#x961F;&#x957F;&apos;</span>, <span class="hljs-string">&apos;weapon&apos;</span>: <span class="hljs-string">&apos;&#x76FE;&apos;</span>}
print(<span class="hljs-string">&apos;&#x6211;&#x662F;{}, &#x6211;&#x6015;{weapon}&#x3002;&apos;</span>.format(*hulk, **captain))
print(<span class="hljs-string">&apos;&#x6211;&#x662F;{name}, &#x6211;&#x6015;{1}&#x3002;&apos;</span>.format(*hulk, **captain))

<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x662F;&#x7EFF;&#x5DE8;&#x4EBA;, &#x6211;&#x6015;&#x76FE;&#x3002;
&#x6211;&#x662F;&#x7F8E;&#x56FD;&#x961F;&#x957F;, &#x6211;&#x6015;&#x62F3;&#x5934;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x540C;&#x65F6;&#x4F7F;&#x7528;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x3001;&#x5143;&#x7EC4;&#x3001;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x3001;&#x5B57;&#x5178;&#x4F20;&#x53C2;</span>
<span class="hljs-comment"># &#x6CE8;&#x610F;&#xFF1A;</span>
<span class="hljs-comment"># &#x4F4D;&#x7F6E;&#x53C2;&#x6570;&#x8981;&#x5728;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x524D;&#x9762;</span>
<span class="hljs-comment"># *&#x5143;&#x7EC4;&#x8981;&#x5728;**&#x5B57;&#x5178;&#x524D;&#x9762;</span>
tup = <span class="hljs-string">&apos;&#x9E70;&#x773C;&apos;</span>,
dic = {<span class="hljs-string">&apos;weapon&apos;</span>: <span class="hljs-string">&apos;&#x7BAD;&apos;</span>}
text = <span class="hljs-string">&apos;&#x6211;&#x662F;{1}&#xFF0C;&#x6211;&#x6015;{weakness}&#x3002;&#x6211;&#x662F;{0}&#xFF0C;&#x6211;&#x7528;{weapon}&#x3002;&apos;</span>
text = text.format(
    *tup, <span class="hljs-string">&apos;&#x9ED1;&#x5BE1;&#x5987;&apos;</span>, weakness=<span class="hljs-string">&apos;&#x7537;&#x4EBA;&apos;</span>, **dic)
print(text)
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x662F;&#x9ED1;&#x5BE1;&#x5987;&#xFF0C;&#x6211;&#x6015;&#x7537;&#x4EBA;&#x3002;&#x6211;&#x662F;&#x9E70;&#x773C;&#xFF0C;&#x6211;&#x7528;&#x7BAD;&#x3002;
&quot;&quot;&quot;</span>
</code></pre>
<h3 id="22-&#x590D;&#x5408;&#x5B57;&#x6BB5;&#x540D;">2.2 &#x590D;&#x5408;&#x5B57;&#x6BB5;&#x540D;</h3>
<ul>
<li><p>&#x540C;&#x65F6;&#x4F7F;&#x7528;&#x4E86;&#x6570;&#x5B57;&#x548C;&#x53D8;&#x91CF;&#x540D;&#x4E24;&#x79CD;&#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x6BB5;&#x540D;&#x5C31;&#x662F;<strong>&#x590D;&#x5408;&#x5B57;&#x6BB5;&#x540D;</strong>&#x3002;</p>
</li>
<li><p>&#x590D;&#x5408;&#x5B57;&#x6BB5;&#x540D;</p>
<p>&#x652F;&#x6301;&#x4E24;&#x79CD;&#x64CD;&#x4F5C;&#x7B26;&#xFF1A;</p>
<ul>
<li><code>.</code> &#x70B9;&#x53F7;</li>
<li><code>[]</code> &#x4E2D;&#x62EC;&#x53F7;</li>
</ul>
</li>
</ul>
<h4 id="221-&#x4F7F;&#x7528;-&#x70B9;&#x53F7;">2.2.1 &#x4F7F;&#x7528;<code>.</code> &#x70B9;&#x53F7;</h4>
<p><strong>&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</strong></p>
<ul>
<li>&#x66FF;&#x6362;&#x5B57;&#x6BB5;&#x5F62;&#x5F0F;&#xFF1A;<code>{&#x6570;&#x5B57;.&#x5C5E;&#x6027;&#x540D;}</code></li>
<li>&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x66FF;&#x6362;&#x5B57;&#x6BB5;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x6570;&#x5B57;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age,gender)</span>:</span>
    self.name = name
    self.age = age
    self.gender = gender
p = Person(<span class="hljs-string">&apos;zhangsan&apos;</span>,<span class="hljs-number">18</span>,<span class="hljs-string">&apos;female&apos;</span>)
print(<span class="hljs-string">&apos;&#x59D3;&#x540D;&#x662F;{0.name},&#x5E74;&#x9F84;&#x662F;{0.age},&#x6027;&#x522B;&#x662F;{0.gender}&apos;</span>.format(p))
print(<span class="hljs-string">&apos;&#x59D3;&#x540D;&#x662F;{.name}&apos;</span>.format(p))  <span class="hljs-comment"># &#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x66FF;&#x6362;&#x5B57;&#x6BB5;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x6570;&#x5B57;</span>
</code></pre>
<h4 id="22-&#x4F7F;&#x7528;&#x4E2D;&#x62EC;&#x53F7;">2.2. &#x4F7F;&#x7528;<code>[]</code>&#x4E2D;&#x62EC;&#x53F7;</h4>
<ul>
<li>&#x7528;&#x5217;&#x8868;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</li>
<li>&#x7528;&#x5143;&#x7EC4;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</li>
<li>&#x7528;&#x5B57;&#x5178;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment"># &#x4E2D;&#x62EC;&#x53F7;&#x7528;&#x6CD5;&#xFF1A;&#x7528;&#x5217;&#x8868;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</span>
infos = [<span class="hljs-string">&apos;&#x963F;&#x661F;&apos;</span>, <span class="hljs-number">9527</span>]
food = [<span class="hljs-string">&apos;&#x9738;&#x738B;&#x82B1;&apos;</span>, <span class="hljs-string">&apos;&#x7206;&#x7C73;&#x82B1;&apos;</span>]
print(<span class="hljs-string">&apos;&#x6211;&#x53EB;{0[0]}&#xFF0C;&#x8B66;&#x53F7;{0[1]}&#xFF0C;&#x7231;&#x5403;{1[0]}&#x3002;&apos;</span>.format(
    infos, food))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x53EB;&#x963F;&#x661F;&#xFF0C;&#x8B66;&#x53F7;9527&#xFF0C;&#x7231;&#x5403;&#x9738;&#x738B;&#x82B1;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x4E2D;&#x62EC;&#x53F7;&#x7528;&#x6CD5;&#xFF1A;&#x7528;&#x5143;&#x7EC4;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</span>
food = (<span class="hljs-string">&apos;&#x50F5;&#x5C38;&apos;</span>, <span class="hljs-string">&apos;&#x8111;&#x5B50;&apos;</span>)
print(<span class="hljs-string">&apos;&#x6211;&#x53EB;{0[0]}&#xFF0C;&#x5E74;&#x9F84;{1}&#xFF0C;&#x7231;&#x5403;{0[1]}&#x3002;&apos;</span>.format(
    food, <span class="hljs-number">66</span>))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x53EB;&#x50F5;&#x5C38;&#xFF0C;&#x5E74;&#x9F84;66&#xFF0C;&#x7231;&#x5403;&#x8111;&#x5B50;&#x3002;
&quot;&quot;&quot;</span>

<span class="hljs-comment"># &#x4E2D;&#x62EC;&#x53F7;&#x7528;&#x6CD5;&#xFF1A;&#x7528;&#x5B57;&#x5178;&#x4F20;&#x9012;&#x4F4D;&#x7F6E;&#x53C2;&#x6570;</span>
dic = dict(name=<span class="hljs-string">&apos;&#x963F;&#x661F;&apos;</span>, pid=<span class="hljs-number">9527</span>)
print(<span class="hljs-string">&apos;&#x6211;&#x662F;{[name]}&#xFF01;&apos;</span>.format(
    dic))
<span class="hljs-comment"># &#x591A;&#x4E2A;&#x66FF;&#x6362;&#x5B57;&#x6BB5;&#xFF0C;&#x4E0D;&#x80FD;&#x7701;&#x7565;&#x6570;&#x5B57;</span>
print(<span class="hljs-string">&apos;&#x6211;&#x662F;{0[name]}&#xFF0C;&#x8B66;&#x53F7;{0[pid]}&#x3002;&apos;</span>.format(
    dic))
<span class="hljs-string">&quot;&quot;&quot;
&#x6211;&#x662F;&#x963F;&#x661F;&#xFF01;
&#x6211;&#x662F;&#x963F;&#x661F;&#xFF0C;&#x8B66;&#x53F7;9527&#x3002;
&quot;&quot;&quot;</span>
</code></pre>
<h2 id="3-&#x8F6C;&#x6362;&#x5B57;&#x6BB5;">3. &#x8F6C;&#x6362;&#x5B57;&#x6BB5;</h2>
<p><strong>&#x8F6C;&#x6362;&#x5B57;&#x6BB5;</strong> <code>conversion field</code> &#x7684;&#x53D6;&#x503C;&#x6709;&#x4E09;&#x79CD;&#xFF0C;&#x524D;&#x9762;&#x8981;&#x52A0; <code>!</code>&#xFF1A;</p>
<ul>
<li><code>s</code>&#xFF1A;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x4E4B;&#x524D;&#x5148;&#x5BF9;&#x53C2;&#x6570;&#x8C03;&#x7528; <code>str()</code></li>
<li><code>r</code>&#xFF1A;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x4E4B;&#x524D;&#x5148;&#x5BF9;&#x53C2;&#x6570;&#x8C03;&#x7528; <code>repr()</code></li>
<li><code>a</code>&#xFF1A;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x4E4B;&#x524D;&#x5148;&#x5BF9;&#x53C2;&#x6570;&#x8C03;&#x7528; <code>ascii()</code></li>
</ul>
<blockquote>
<p> <code>ascii()</code> &#x51FD;&#x6570;&#x7C7B;&#x4F3C; <code>repr()</code> &#x51FD;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x8868;&#x793A;&#x5BF9;&#x8C61;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;
 &#x4F46;&#x662F;&#x5BF9;&#x4E8E;&#x975E; <code>ASCII</code> &#x5B57;&#x7B26;&#xFF0C;&#x4F7F;&#x7528; <code>\x</code>&#xFF0C;<code>\u</code> &#x6216;&#x8005; <code>\U</code> &#x8F6C;&#x4E49;&#x3002;</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># &#x8F6C;&#x6362;&#x5B57;&#x6BB5;</span>
print(<span class="hljs-string">&apos;I am {!s}!&apos;</span>.format(<span class="hljs-string">&apos;Bruce Lee &#x674E;&#x5C0F;&#x9F99;&apos;</span>))
print(<span class="hljs-string">&apos;I am {!r}!&apos;</span>.format(<span class="hljs-string">&apos;Bruce Lee &#x674E;&#x5C0F;&#x9F99;&apos;</span>))
print(<span class="hljs-string">&apos;I am {!a}!&apos;</span>.format(<span class="hljs-string">&apos;Bruce Lee &#x674E;&#x5C0F;&#x9F99;&apos;</span>))
<span class="hljs-string">&quot;&quot;&quot;
I am Bruce Lee &#x674E;&#x5C0F;&#x9F99;!
I am &apos;Bruce Lee &#x674E;&#x5C0F;&#x9F99;&apos;!
I am &apos;Bruce Lee \u674e\u5c0f\u9f99&apos;!
&quot;&quot;&quot;</span>
</code></pre>
<h2 id="4-&#x683C;&#x5F0F;&#x8BF4;&#x660E;&#x7B26;">4. &#x683C;&#x5F0F;&#x8BF4;&#x660E;&#x7B26;</h2>
<p>&#x683C;&#x5F0F;&#x8BF4;&#x660E;&#x7B26;&#x4F7F;&#x7528;&#x8FC7;&#x4E8E;&#x590D;&#x6742;&#xFF0C;&#x4E14;&#x5B9E;&#x9645;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x4E0D;&#x591A;&#xFF0C;&#x6682;&#x4E0D;&#x8BA8;&#x8BBA;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter05.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 成员运算符"><i class="fa fa-angle-left"></i></a>
        
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
